package com.hy.main.demo.controller;

import cn.hutool.core.date.DateUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author yang DongYong
 * @Date 2022/5/12 - 05 - 12 - 1:18
 * @Description: com.hy.main.demo.controller
 * @version: 1.0
 * 发送延迟消息
 */
@RestController
@Slf4j
@AllArgsConstructor
public class SendMesController {

    private RabbitTemplate rabbitTemplate;

    @GetMapping("/ttl/sendMes/{message}")
    public String sendMes(@PathVariable String message) {
        log.info("发送的消息为:{}",DateUtil.now() +"|"+message);
        rabbitTemplate.convertAndSend("X","XA","RoutingKey为XA发送的消息 10s:"+message);
        rabbitTemplate.convertAndSend("X","XB","RoutingKey为XA发送的消息 40s:"+message);
        return message;
    }

    /**
     * 自定义延迟队列的延迟时间
     * @param message 消息内容
     * @param ttlTime 延迟时间
     * @return 消息内容
     */
    @GetMapping("/ttl/sendMes/{message}/{ttlTime}")
    public String sendMes(@PathVariable String message,@PathVariable String ttlTime) {
        log.info("发送的消息为:{}",DateUtil.now() +"|"+message);
        rabbitTemplate.convertAndSend("X","XC","RoutingKey为XC发送的消息:"+message,
                time -> {time.getMessageProperties().setExpiration(ttlTime);
                return time;
        });
        return message;
    }
}
